<template>
  <el-dialog :title="formData.id ? '编辑房产' : '添加房产' " :visible="showVisible" @close="resetForm">
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="分类">
        <el-select v-model="formData.category">
          <el-option v-for="(item,index) in sortList" :key="index" :value="item.id" :label="item.title" />
        </el-select>
      </el-form-item>
      <el-form-item label="经济">
        <el-select v-model="formData.agent">
          <el-option v-for="item in brokerList" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" type="text" />
      </el-form-item>
      <el-form-item label="标签">
        <el-tag
          v-for="tag in formData.tags"
          :key="tag"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          class="input-new-tag"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        />
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      </el-form-item>
      <el-form-item label="地址">
        <div style="margin-top: 15px;">
          <el-cascader
            v-model="formData.address"
            :options="options"
            @change="handleChange"
          />
          <el-input v-model="value" @change="handleChange" />
        </div>
      </el-form-item>
      <el-form-item label="价格">
        <el-input v-model="formData.price" type="text" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="formData.content" />
      </el-form-item>
      <!-- 介绍图片 -->
      <el-form-item label="介绍图片" prop="image">
        <Uploadl ref="uploadImg1" />
      </el-form-item>
      <!-- 封面 -->
      <!-- 上传图片 -->
      <el-form-item label="封面" prop="image">
        <Uploadl ref="uploadImg2" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="addFn">确 定</el-button>
      <el-button @click="resetForm">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import address from '@/address.json'
import { getclassifycation } from '@/api/classification'
import { addhousemount, editorhousemount, geteconomicslist, gethouseamountId } from '@/api/propertyManagement'

export default {
  props: {
    showVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '', // 标题
        desc: '', // 描述
        image: '', // 图片
        agent: 38, // 经济id
        content: '', // 内容
        address: '', // 地址
        price: '', // 价格
        photos: [], // 图片
        tags: [], // 标签，字符串组成数组
        category: 5// 分类id
      },
      inputVisible: false,
      inputValue: '', // 标签输入框
      brokerList: [], // 经济列表
      sortList: [], // 分类列表
      options: [], // 地址数据
      // 表单验证
      rules: {
        title: [
          { required: true, message: '请输入房产标题', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入房产描述', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入房产描述', trigger: 'blur' }
        ]
      },
      value: ''
    }
  },
  async created() {
    const res = await geteconomicslist()// 经济列表
    // console.log(111, res)
    this.brokerList = res.data
    const list = await getclassifycation()// 分类列表
    // console.log(222, list)
    this.sortList = list.data
    this.handleChange()// 页面一打开的时候调用
  },
  methods: {
    // 省市区
    handleChange() {
      // 使用map遍历把对应的省市区给到element-ui规定的属性
      this.options = address.data.map(item => {
        return {
          label: item.province,
          value: item.province,
          children: item.city.map(val => {
            return {
              label: val.city,
              value: val.city,
              children: val.district.map(i => {
                return {
                  label: i.district,
                  value: i.district
                }
              })
            }
          })
        }
      })
      console.log(152, this.options)
    },
    // 新增房产
    async addFn() {
      await this.$refs.form.validate()
      this.formData.photos[0] = this.$refs.uploadImg1.fileList.length && this.$refs.uploadImg1.fileList[0].url
      this.formData.image = this.$refs.uploadImg2.fileList.length && this.$refs.uploadImg2.fileList[0].url
      // console.log('通过验证')
      if (this.formData.id) {
        // 编辑接口
        await editorhousemount(this.formData)
      } else {
        // 新增接口
        await addhousemount(this.formData)
      }
      // 渲染
      this.$emit('addEvents')
      // 关闭弹窗
      this.resetForm()
    },
    // 关闭弹窗---清空表单重置验证
    resetForm() {
      // 关闭弹窗
      this.$emit('update:showVisible', false)
      // 重置效验--清空表单
      this.$refs.form.resetFields()
      this.formData = {
        title: '', // 标题
        desc: '', // 描述
        image: '', // 图片
        agent: 0, // 经济id
        content: '', // 内容
        address: '', // 地址
        price: '', // 价格
        photos: [], // 图片
        tags: [], // 标签，字符串组成数组
        category: 5// 分类id
      }
      this.$refs.uploadImg1.removeFn()
      this.$refs.uploadImg2.removeFn()
    },
    // 编辑回显
    async geteditor(id) {
      const res = await gethouseamountId(id)
      // console.log(150, res)
      this.formData = res.data
      // 图片回显
      if (this.formData.image) {
        this.$refs.uploadImg1.fileList.push({
          url: this.formData.image
        })
      }
      if (this.formData.image) {
        this.$refs.uploadImg2.fileList.push({
          url: this.formData.photos
        })
      }
    },
    // 删除标签
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1)
    },
    // 展示输入框
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 确认输入框添加标签
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.formData.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>

<style scoped>
 .el-select .el-input {
    width: 130px;
  }
  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
